package com.sugar.real.estate.module.business.dal.mysql.readyhouse;

import cn.hutool.core.util.StrUtil;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.real.estate.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.real.estate.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.real.estate.module.business.controller.admin.readyhouse.vo.ReadyHousePageReqVO;
import com.sugar.real.estate.module.business.controller.admin.readyhouse.vo.ReadyHouseRespVO;
import com.sugar.real.estate.module.business.controller.app.readyhouse.vo.AppReadyHousePageReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.readyhouse.ReadyHouseDO;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 现房 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface ReadyHouseMapper extends BaseMapperX<ReadyHouseDO> {

    default PageResult<ReadyHouseRespVO> selectPage(ReadyHousePageReqVO reqVO) {
        String name = reqVO.getNameZhCn();
        String salesMethod = reqVO.getSalesMethodZhCn();
        Long[] provinceIds = reqVO.getProvinceId();
        Long provinceId = null;
        Long cityId = null;
        Long countyId = null;
        if (Objects.nonNull(provinceIds)) {
            int length = provinceIds.length;
            switch (length){
                case 3:
                    countyId = provinceIds[2];
                case 2:
                    cityId = provinceIds[1];
                case 1:
                    provinceId = provinceIds[0];
            }
        }
        return selectJoinPage(reqVO, ReadyHouseRespVO.class, new MPJLambdaWrapperX<ReadyHouseDO>()
                .selectAll(ReadyHouseDO.class)
                .eqIfPresent(ReadyHouseDO::getHouseType, reqVO.getHouseType())
                .eqIfPresent(ReadyHouseDO::getProjectType, reqVO.getProjectType())
                .eqIfPresent(ReadyHouseDO::getProvinceId, provinceId)
                .eqIfPresent(ReadyHouseDO::getCityId, cityId)
                .eqIfPresent(ReadyHouseDO::getCountyId, countyId)
                .betweenIfPresent(ReadyHouseDO::getCreateTime, reqVO.getCreateTime())
                .nested(StrUtil.isNotBlank(name),
                        o -> o.like(ReadyHouseDO::getNameZhCn, name)
                                .or().like(ReadyHouseDO::getNameEnUs, name))
                .nested(StrUtil.isNotBlank(salesMethod),
                        o -> o.like(ReadyHouseDO::getSalesMethodZhCn, salesMethod)
                                .or().like(ReadyHouseDO::getSalesMethodEnUs, salesMethod))
                .orderByDesc(ReadyHouseDO::getId));
    }

    default PageResult<ReadyHouseDO> appSelectPage(AppReadyHousePageReqVO reqVO){
        String name = reqVO.getName();

        BigDecimal priceMin = null;
        BigDecimal priceMax = null;
        String price = reqVO.getPrice();
        if (StrUtil.isNotBlank(price)){
            String[] split = price.split("-");
            if (split.length == 2){
                String val1 = split[0].trim();
                if (StrUtil.isNotBlank(val1))
                    priceMin = new BigDecimal(val1);
                String val2 = split[1].trim();
                if (StrUtil.isNotBlank(val2))
                    priceMax = new BigDecimal(val2);
            }else if (split.length == 1){
                String val1 = split[0].trim();
                if (StrUtil.isNotBlank(val1))
                    priceMin = new BigDecimal(val1);
            }
        }

        String area = reqVO.getArea();
        BigDecimal areaMin = null;
        BigDecimal areaMax = null;
        if (StrUtil.isNotBlank(area)){
            String[] split = area.split("-");
            if (split.length == 2){
                String val1 = split[0].trim();
                if (StrUtil.isNotBlank(val1))
                    areaMin = new BigDecimal(val1);
                String val2 = split[1].trim();
                if (StrUtil.isNotBlank(val2))
                    areaMax = new BigDecimal(val2);
            }else if (split.length == 1){
                String val1 = split[0].trim();
                if (StrUtil.isNotBlank(val1))
                    areaMin = new BigDecimal(val1);
            }
        }

        BigDecimal finalPriceMin = priceMin;
        BigDecimal finalPriceMax = priceMax;
        return selectPage(reqVO, new LambdaQueryWrapperX<ReadyHouseDO>()
                .eqIfPresent(ReadyHouseDO::getHouseType, reqVO.getHouseType())
                .eqIfPresent(ReadyHouseDO::getProjectType, reqVO.getProjectType())
                .eqIfPresent(ReadyHouseDO::getProvinceId, reqVO.getProvinceId())
                .eqIfPresent(ReadyHouseDO::getCityId, reqVO.getCityId())
                .eqIfPresent(ReadyHouseDO::getCountyId, reqVO.getCountyId())
                .eqIfPresent(ReadyHouseDO::getIsHot, reqVO.getIsHot())
                .betweenIfPresent(ReadyHouseDO::getHouseArea, reqVO.getHouseAreaMin(), reqVO.getHouseAreaMax())
                .geIfPresent(ReadyHouseDO::getHouseArea, areaMin)
                .leIfPresent(ReadyHouseDO::getHouseArea, areaMax)
                .eqIfPresent(ReadyHouseDO::getIsShow, 1)
                .ge(Objects.nonNull(finalPriceMin), ReadyHouseDO::getPriceMin, finalPriceMin)
                .le(Objects.nonNull(finalPriceMax), ReadyHouseDO::getPriceMin, finalPriceMax)
                /*.nested(Objects.nonNull(priceMin),
                        o -> o.ge(ReadyHouseDO::getPriceMin, finalPriceMin).or()
                                .ge(ReadyHouseDO::getPriceMax, finalPriceMin))
                .nested(Objects.nonNull(priceMax),
                        o -> o.le(ReadyHouseDO::getPriceMin, finalPriceMax).or()
                                .le(ReadyHouseDO::getPriceMax, finalPriceMax))*/
                .nested(StrUtil.isNotBlank(name),
                        o -> o.like(ReadyHouseDO::getNameZhCn, name)
                                .or().like(ReadyHouseDO::getNameEnUs, name))
                .orderByDesc(ReadyHouseDO::getId));
    }
}